package com.qf.service.impl;

import com.qf.dao.C_vDao;
import com.qf.dao.CouponDao;
import com.qf.dao.VipDao;
import com.qf.dao.impl.C_vDaoImpl;
import com.qf.dao.impl.CouponDaoImpl;
import com.qf.dao.impl.VipDaoImpl;
import com.qf.entity.C_v;
import com.qf.entity.Coupon;
import com.qf.entity.Vip;
import com.qf.service.C_vService;
import com.qf.utils.DBUtils;
import com.qf.utils.PageUtils;
import com.sun.javaws.IconUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class C_vServiceImpl implements C_vService {
    private C_vDao c_vDao = new C_vDaoImpl();
    private CouponDao couponDao = new CouponDaoImpl();
    private VipDao vipDao = new VipDaoImpl();

    /**
     * 增加记录条数
     *
     * @param c_vs
     * @return
     */
    @Override
    public int addC_v(C_v[] c_vs) {
        //设置一个数判断是否能添加
        //用一个集合存储传过来的数据
        if (c_vs == null || c_vs.length == 0) {
            return -1;
        }
        List<C_v> c_vList = new ArrayList<>();
        for (int i = 0; i < c_vs.length; i++) {
            try {
                c_vList.add(c_vs[i]);
                //判断是否已经有这条记录，有的话就不能添加
                C_v c_v = null;
                c_v = c_vDao.selectC_vByVidAndCid(c_vs[i].getC_v_vid(), c_vs[i].getC_v_cid());
                //当它不为空说明该记录已经存在,如果有一条记录已经存在则都不添加
                if (c_v != null) {
                    return -1;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return -1;
            }
        }

        Connection con = null;
        int count = 0;
        try {
            con = DBUtils.DB.getQr().getDataSource().getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < c_vList.size(); i++) {
                //添加数据
                int num = c_vDao.addC_v(c_vList.get(i));
                count++;
            }
        } finally {
            //无论如何都会先判断是否同时增加成功,若count为记录数就增加成功，否则增加失败
            if (count != c_vList.size()) {
                try {
                    assert con != null;
                    con.rollback();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    con.commit();
                    con.setAutoCommit(true);
                    return count;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            try {
                con.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return -1;
        }

    }

    /**
     * 用于展示记录条数
     *
     * @param currentPageNoStr
     * @param pageSizeStr
     * @return
     */
    @Override
    public PageUtils<C_v> getC_vPageUtils(String currentPageNoStr, String pageSizeStr) {
        //处理当前页
        Integer currentPageNo = 1;
        if (currentPageNoStr != null && !"".equals(currentPageNoStr)) {
            currentPageNo = Integer.parseInt(currentPageNoStr);
        }
        //处理页量
        Integer pageSize = 5;
        if (pageSizeStr != null && !"".equals(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        int totalPageCount = 0;
        int totalPageSize = 0;
        List<C_v> c_vList = null;
        try {
            //处理总记录数
            totalPageCount = c_vDao.selectC_vCount();
            //处理总页数
            totalPageSize = totalPageCount % pageSize == 0 ? totalPageCount / pageSize : totalPageCount / pageSize + 1;
            //处理总记录数
            c_vList = c_vDao.selectC_vListByPage(currentPageNo, pageSize);
            //非空判断
            if (c_vList != null && c_vList.size() > 0) {
                //循环遍历
                for (C_v c_v : c_vList) {
                    Integer vid = c_v.getC_v_vid();
                    if (vid != null) {
                        Vip vip = vipDao.selectVipByVid(vid);
                        c_v.setVip(vip);
                    }
                    Integer cid = c_v.getC_v_cid();
                    if (cid != null) {
                        Coupon coupon = couponDao.selectCouponByCid(cid);
                        c_v.setCoupon(coupon);
                    }
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageUtils<C_v> pageUtils = new PageUtils<>(currentPageNo, pageSize, totalPageCount, totalPageSize, c_vList);
        return pageUtils;
    }
}
